﻿package com.mj
{
	import flash.net.LocalConnection;
	import flash.events.StatusEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	
	/**
	 * 
	 * @author Ids Klijnsma
	 * ©2008 labs.flexperiments.nl
	 * Use this class to interact with the ASDebugger.
	 * Usage:
	 * ASDebug.debug(<string>) 
	 * or 
	 * ASDebug.debug_prop(<object>);
	 */	
	public class ASDebugger {
		
		private var sender_lc:LocalConnection;
		
		private static var _instance:ASDebugger
		
		public function ASDebugger():void {
			this.sender_lc = new LocalConnection();
			this.sender_lc.addEventListener(StatusEvent.STATUS, ASDebugger.statusHandler);	//will override itself everytime this method is called
			this.sender_lc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, ASDebugger.asyncErrorHandler);
			this.sender_lc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ASDebugger.securityErrorHandler);
		}
		
		private static function get instance():ASDebugger {
			if(!_instance) _instance = new ASDebugger();
			return _instance;
		}
		
		/**
		 * Send a string to the debugger 
		 * @param value the string to debug
		 * 
		 */		
		public static function debug(value:String):void {
			ASDebugger.instance.send("debug", value); 
		}
		
		/**
		 * Send an object to the debugger 
		 * @param value The object to debug
		 * 
		 */		
		public static function debug_prop(value:*):void {
			//ASDebugger.instance.send("debug_prop", value);
		}
		
		private function send(method:String, value:*):void {
			this.sender_lc.send("_asDebug", method, value);
		}
		
		private static function statusHandler(event:StatusEvent):void {}	//just to prevent errors if the debugger is not running
		private static function asyncErrorHandler(event:AsyncErrorEvent):void {}
		private static function securityErrorHandler(event:SecurityErrorEvent):void {}
	}
}